package com.leadbank.baselbf.outils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;

import com.leadbank.baselbf.base.BaseApplication;
import com.leadbank.baselbf.leadutils.LbwADIUtils;
import com.leadbank.baselbf.log.LogBus;

import java.util.List;

/**
 * @ClassName: AndroidUtils
 * @Description:
 * @Author: zhouzhiguo
 * @Date: 2024/5/27 18:17
 * @Version: 5.0.6
 * 上海利得金融科技集团版权所有.
 */

public class AndroidUtils {
    private static String tag = "AndroidUtils";


    /**
     * 获取当前栈顶 Activity
     *
     * @param context
     * @return
     */
    public static String getTopActivityName(Context context) {
        try {
            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningTaskInfo> taskList = activityManager.getRunningTasks(1);
            if (taskList == null || taskList.isEmpty()) {
                return "NoActivity";
            }
            ComponentName topActivity = taskList.get(0).topActivity;
            String packageName = topActivity.getPackageName();
            String className = topActivity.getClassName();
            return className;
        } catch (Exception e) {
            e.printStackTrace();
            return "NoActivity";
        }

    }


    /**
     * 去应用市场升级
     * <p>
     * 应用宝：tmast://appdetails?pname=com.tencent.mm；
     * 小米：mimarket://details?id=com.tencent.mm；
     * 三星：samsungapps://ProductDetail/com.tencent.mm；
     * 华为：appmarket://details?id=com.tencent.mm；
     * oppo：oppomarket://details?packagename=com.tencent.mm；
     * vivo：vivomarket://details?id=com.tencent.mm；
     *
     * @param activity
     */
    public static void goToAPPMarket(Activity activity) {
        if (activity == null) {
            return;
        }

        try {
            String uriString = "";

            String brand = getBrand();
            if (LbwADIUtils.isNvl(brand)){
                LogBus.d(tag,"去网页版的应用宝");
                //如果都没有，就去网页版的应用宝
                String url = "https://sj.qq.com/appdetail/"+ activity.getPackageName();
                AndroidUtils.goToWebBrowser(activity,url);
                return;
            }

            if (brand.equals("HUAWEI") ){
                uriString = "appmarket://details?id=" + activity.getPackageName();
                LogBus.d(tag,"去华为应用商店");
//            }else if (brand.equals("XIAOMI")){
//                uriString = "mimarket://details?id=" + activity.getPackageName();
//                LogBus.d(tag,"去小米应用市场");
            }else if (brand.equals("OPPO")){
                uriString = "oppomarket://details?packagename=" + activity.getPackageName();
                LogBus.d(tag,"去OPPO应用商店");
            }else if (brand.equals("VIVO")){
                uriString = "vivomarket://details?id=" + activity.getPackageName();
                LogBus.d(tag,"去vivo应用商店");
            }else{
                LogBus.d(tag,"去网页版的应用宝");
                //如果都没有，就去网页版的应用宝
                String url = "https://sj.qq.com/appdetail/"+ activity.getPackageName();
                AndroidUtils.goToWebBrowser(activity,url);
                return;
            }
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(uriString));
            activity.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            LogBus.d(tag,"去网页版的应用宝");
            //如果都没有，就去网页版的应用宝
            String url = "https://sj.qq.com/appdetail/"+ activity.getPackageName();
            AndroidUtils.goToWebBrowser(activity,url);
        }
    }


    /**
     * 去默认浏览器打开
     * @param activity
     * @param updateUrl
     */
    public static void goToWebBrowser(Activity activity, String updateUrl) {
        if (activity == null) {
            return;
        }

        try {
            // 创建一个Intent对象，指定ACTION_VIEW动作
            Intent browserIntent = new Intent(Intent.ACTION_VIEW);
            // 创建一个Uri对象，传入你要打开的URL
            Uri uri = Uri.parse(updateUrl);
            // 将Uri设置给Intent的数据
            browserIntent.setData(uri);
            activity.startActivity(browserIntent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 品牌（Brand）
     *
     * @return 品牌
     */
    public static String getBrand() {
        String brand = android.os.Build.BRAND;
        String normalizedBrand = brand.toUpperCase();;
        LogBus.d(tag,"手机品牌 = "+ brand);
        return normalizedBrand;
    }

    /**
     * 获取手机型号
     *
     * @return
     */
    public static String getDeviceModel() {
        return Build.MODEL;
    }

    /**
     * 获取系统版本
     *
     * @return
     */
    public static String getDeviceRelease() {
        return Build.VERSION.RELEASE;
    }

    /**
     * 获取手机厂商名称
     *
     * @return
     */
    public static String getDeviceBrand() {
        return Build.BRAND;
    }

    /**
     * 获取手机内部识别码
     *
     * @return
     */
    public static String getDeviceFingerprint() {
        return Build.FINGERPRINT;
    }


    /**
     * 获取网络类型
     *
     * @return
     */
    public static String getNetworkType() {
        ConnectivityManager man = (ConnectivityManager) BaseApplication.getBaseApplication().getSystemService(Context.CONNECTIVITY_SERVICE);

        @SuppressLint("MissingPermission") NetworkInfo ni = man.getActiveNetworkInfo();
        String subtypeName = "";
        if (ni != null) {

            if (ni.getType() == ConnectivityManager.TYPE_WIFI) {
                subtypeName = "WIFI";
            } else if (ni.getType() == ConnectivityManager.TYPE_MOBILE) {
                subtypeName = "" + ni.getSubtypeName();
            }
        } else {
            LogBus.e(tag, "NetworkInfo  no");
        }

        return subtypeName;
    }
}
